/**********************************************************************************
 * Code license:  GNU General Public License v3                                   *
 * Main page:     https://code.google.com/p/library-cross-platform/               *
 * Issue tracker: https://code.google.com/p/library-cross-platform/issues/list    *
***********************************************************************************/

package library.util;

import java.lang.reflect.InvocationTargetException;

@SuppressWarnings({})
public class EventHandlerExt extends EventHandler {
	
  public    final static java.lang.String EVENT_EXIT              = "EVENT_EXIT";
  protected static       Hash             calcHash                = new Hash();
  protected final static long             eventExitHash           = calcHash.getStringHashValue(EventHandlerExt.EVENT_EXIT);
  protected              java.lang.String startEvent;
  protected static       java.lang.String exitEventListener       = null;

  public EventHandlerExt() {
    exitEventListener = (exitEventListener == null) ? new EventExitDefault().getClass().getName() : exitEventListener;
    registrateEvent(EventHandlerExt.EVENT_EXIT, exitEventListener);	  
  }

  public EventHandlerExt(java.lang.String startEvent) {
    this();	  
    setStartEvent(startEvent);
  }
  
  public void setStartEvent(java.lang.String startEvent) {
    this.startEvent = startEvent;
  }

  public boolean startEventStates() throws InvocationTargetException,
                                           NoSuchMethodException,
                                           SecurityException {
   EventListenerExt eventListener;
   java.lang.String    eventClassName;

   try {
     setNextEvent(startEvent);

     while(true) {
       eventClassName = getEventClassName(getNextEvent());
       eventListener = (EventListenerExt)getEventListener(eventClassName);
       eventListener.setParent(this);
       eventListener.start();

       if(calcHash.getStringHashValue(getNextEvent()) == eventExitHash) {
         eventClassName = getEventClassName(getNextEvent());
         eventListener = (EventListenerExt)getEventListener(eventClassName);
         eventListener.setParent(this);
         eventListener.start();
         break;
       }
     } /* while() end */
   } catch(IllegalEventException e) {
     e.printStackTrace();
   } catch (IllegalAccessException e) {
     e.printStackTrace();
   } catch (InstantiationException e) {
     e.printStackTrace();
   } catch(ClassNotFoundException e) {
     e.printStackTrace();
   }
   return false;
  }

  public class EventExitDefault extends EventListenerExt {

   public boolean start() throws IllegalEventException {
     return false;
   }
  }
}
